<template>
  <div>
    <el-dialog
      :title="title"
      :close-on-click-modal="false"
      :visible.sync="visible"
      width="700px"
    >
      <el-form
        :model="dataForm"
        :rules="dataFormRules"
        ref="dataForm"
        label-width="80px"
      >
        <el-row>
          <el-col :span="20">
            <el-form-item
              :label="mode == 'sales' ? '销售人员' : '运营人员'"
              prop="staff"
            >
              <nice-select
                v-model="dataForm.staff"
                :placeholder="placeholder"
                clearable
                :options="list"
              ></nice-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false" size="small">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit" size="small"
          >确定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { tableUpdate, queryStaffListByType } from '@/api/api'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        staff: ''
      },
      dataFormRules: {
        staff: [{ required: true, message: '', trigger: 'change' }]
      },
      placeholder: '',
      title: '',
      list: []
    }
  },
  props: {
    mode: {
      type: String,
      default: ''
    },
    multipleSelection: {
      type: Array,
      default: []
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.placeholder =
          this.mode === 'sales' ? '请选择销售人员' : '请选择运营人员'
        this.dataFormRules.staff[0].message =
          this.mode === 'sales' ? '请选择销售人员' : '请选择运营人员'
        this.title = this.mode === 'sales' ? '分配销售' : '分配运营'
        this.$refs['dataForm'].resetFields()
        // 6代表销售人员，7代表运营人员
        let type = this.mode === 'sales' ? '6' : '7'
        this.queryStaffListByType(type)
      })
    },
    async queryStaffListByType (type) {
      let res = await queryStaffListByType({ 'sys_user.user_type': type })
      if (res[0]) {
        return
      }
      let data = res[1]
      if (data && data.code === 0) {
        this.list = data.data.list.map(ele => {
          return {
            label: ele.name,
            value: ele.user_id
          }
        })
      } else {
        this.$message.error(data.msg)
      }
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          let list = []
          list = this.multipleSelection.map(ele => {
            return {
              table_name: 'nice_school',
              update: [
                this.mode === 'sales'
                  ? { market_id: this.dataForm.staff }
                  : { operation_id: this.dataForm.staff }
              ],
              where: [
                {
                  id: ele.id
                }
              ]
            }
          })
          this.submit(list)
        }
      })
    },
    async submit (e) {
      let res = await tableUpdate(e)
      if (res[0]) {
        return
      }
      let data = res[1]
      if (data && data.code === 0) {
        this.$message({
          message: '分配成功',
          type: 'success',
          duration: 1500,
          onClose: () => {
            this.visible = false
            this.$emit('refreshDataList')
          }
        })
      } else {
        this.$message.error(data.msg)
      }
    }
  }
}
</script>
